package miku.firstgame.ui.component
{
	import com.saia.starlingPunk.SP;
	
	
	import feathers.controls.Button;
	import feathers.controls.TextInput;
	import feathers.core.FeathersControl;
	
	import miku.firstgame.constrants.EventConstants;
	import miku.firstgame.util.StringUtil;
	
	import starling.display.Quad;
	import starling.events.Event;

	public class MessagePanel extends FeathersControl
	{
		private var outputBack:Quad;

		public var messageOutput:MessageOutput;
		public var messageInput:TextInput;
		public var messageSendButton:Button;

		public function MessagePanel()
		{
			super();
		}

		protected function createChildren():void
		{
			outputBack = new Quad(100, 100, 0xcccccc);
			outputBack.alpha = 0.2;
			addChild(outputBack);

			messageInput = new TextInput();
//			messageInput.setSize(300, 40);
			messageOutput = new MessageOutput();
//			messageOutput.setSize(380, 200);
			messageSendButton = new Button();
			messageSendButton.addEventListener(Event.TRIGGERED, onMessageSend);
//			messageSendButton.setSize(70, 40);
			messageSendButton.label = "发送";
			addChild(messageInput);
			addChild(messageOutput);
			addChild(messageSendButton);
		}

		private function onMessageSend(e:Event):void
		{
			var value:String = StringUtil.trim(messageInput.text);
			if (value.length > 0)
			{
				dispatchEventWith(EventConstants.LOBBY_MESSAGE_SEND, false, messageInput.text);
				messageInput.text = "";
			}
		}

		override protected function initialize():void
		{
			super.initialize();
			createChildren();
		}

		override protected function draw():void
		{
			super.draw();
			var sizeInvalid:Boolean = this.isInvalid(INVALIDATION_FLAG_SIZE);
			if (sizeInvalid)
			{
				resize();
			}
		}

		public function resize():void
		{
			messageSendButton.setSize(70, 40);
			messageInput.setSize(width - messageSendButton.width - 10, messageSendButton.height);
			messageOutput.setSize(width, height - messageSendButton.height - 10);
			outputBack.width = messageOutput.width;
			outputBack.height = messageOutput.height;

			messageOutput.x = 0;
			messageOutput.y = 0;

			messageInput.x = 0;
			messageInput.y = messageOutput.height + 10;

			messageSendButton.x = messageInput.x + messageInput.width + 10;
			messageSendButton.y = messageInput.y;

		}

		public function appendMessage(name:String, message:String):void
		{
			messageOutput.appendMessage(name, message);
			messageOutput.newLine();
//			messageOutput.validate();  
//			messageOutput.scrollToBottom();
		}

		public function appendSysMessage(message:String):void
		{
			messageOutput.appendSysMessage(message);
			messageOutput.newLine();
//			messageOutput.validate();
//			messageOutput.scrollToBottom();
		}

		public function newLine():void
		{
			messageOutput.newLine();
		}

	}
}
